package com.example.service;

import com.example.entity.AddressBook;
import com.example.entity.Goods;
import com.example.exception.CustomerException;
import com.example.mapper.AddressBookMapper;
import com.example.mapper.GoodsMapper;
import com.example.utils.MapUtils;
import com.example.utils.PriceUtils;
import com.example.vo.PriceVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

@Service
public class GoodsService {

    @Resource
    GoodsMapper goodsMapper;
    @Resource
    MapUtils mapUtils;
    @Resource
    PriceUtils priceUtils;
    @Resource
    AddressBookMapper addressBookMapper;
    public void add(Goods goods) {
        goodsMapper.insert(goods);

    }

    public void deleteById(Integer id) {
        goodsMapper.deleteById(id);
    }

    public void updateById(Goods goods) {
        goodsMapper.updateById(goods);
    }

    public List<Goods> selectAll() {
        return goodsMapper.selectAll();
    }

    public Goods selectById(Integer id) {
        return goodsMapper.selectById(id);
    }

    public PageInfo<Goods> selectPage(Integer pageNum, Integer pageSize) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Goods> list = goodsMapper.selectAll();
        return PageInfo.of(list);
    }

    private String getString(AddressBook addressBook, String Address) {
        try {
            String string;
            if (addressBook.getLongitude() == null || addressBook.getLatitude() == null) {
                Map<String,String> map = mapUtils.getLonAndLat(Address);
                string = map.get("lng")+","+map.get("lat");
                addressBook.setLongitude(new BigDecimal(map.get("lng")));
                addressBook.setLatitude(new BigDecimal(map.get("lat")));
                addressBookMapper.updateById(addressBook);
            }
            else{
                string = addressBook.getLongitude()+","+addressBook.getLatitude();
            }
            return string;
        }catch (Exception e){
            throw new CustomerException("计算价格失败");
        }
    }

    public PriceVO getPrice(Double weight, Integer shippingBookId, Integer deliveryBookId) {
        PriceVO price = new PriceVO();
        AddressBook shippingBook = addressBookMapper.selectById(shippingBookId);
        AddressBook deliveryBook = addressBookMapper.selectById(deliveryBookId);
        String shippingAddress = shippingBook.getAddress();
        String deliveryAddress = deliveryBook.getAddress();
        double distance;
        String origin, destination;
        try {
            origin = getString(shippingBook, shippingAddress);
            destination = getString(deliveryBook, deliveryAddress);
            distance = mapUtils.getDistance(origin,destination) / 1000;
        } catch (Exception e) {
            throw new CustomerException("计算价格失败");
        }
        price.setPrice(BigDecimal.valueOf(priceUtils.calculatePrice(weight, distance)).setScale(2, RoundingMode.HALF_UP));
        price.setDistance(BigDecimal.valueOf(distance));
        return price;
    }


}
